package org.example.code.mobile.popup.actions;

import org.eclipse.core.resources.IFile;
import org.eclipse.emf.common.command.BasicCommandStack;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.mad.code.transformation.ViewTransformation;

import MobileApplicationDeveloper.model.AppMobile;
import MobileApplicationDeveloper.model.View;
import MobileApplicationDeveloper.model.impl.ContainerImpl;
import MobileApplicationDeveloper.model.impl.ModelFactoryImpl;
import MobileApplicationDeveloper.model.util.ModelAdapterFactory;
import command.NewProjectCommand;

public class GenerateApplicationModelAction implements IObjectActionDelegate {

	private Shell shell;
	
	/**
	 * Constructor for Action1.
	 */
	public GenerateApplicationModelAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
//		WizardDialog dialog = new WizardDialog(shell, new YourWizardClass());
//		dialog.open(); 
//		 WizardDialog wizardDialog = new WizardDialog(shell,
//			      new WizardGenerateCode());
//			    if (wizardDialog.open() == Window.OK) {
//			    	MessageDialog.openInformation(
//			    			shell,
//			    			"Mobile Application Developer",
//			    			"Code Generated Succesfully.");
//			      System.out.println("Ok pressed");
//			    } else {
//			    	MessageDialog.openInformation(
//			    			shell,
//			    			"Mobile Application Developer",
//			    			"Code Generate Canceled.");
//			    	
//			       }

				try {

			  		IWorkbenchWindow window = PlatformUI.getWorkbench()
								.getActiveWorkbenchWindow();
			  		
					ISelection selection = window.getSelectionService().getSelection(
							"org.eclipse.ui.navigator.ProjectExplorer");
					
					TreePath[] paths = ((TreeSelection) selection).getPaths();
					TreePath p = paths[0];
					Object last = p.getLastSegment();
	
					if (last instanceof IFile) {
						String ipath = ((IFile) last).getLocationURI().toString();
					
					    ModelFactoryImpl.eINSTANCE.createAppMobile();
					    AdapterFactoryEditingDomain domain = 
					    	      new AdapterFactoryEditingDomain(new ModelAdapterFactory(), new BasicCommandStack());
					    	   Resource resource = domain.createResource(ipath);
					    	   resource.load(null);
					    	   ContainerImpl container = (ContainerImpl)resource.getContents().get(0);
					    	   
					    	   container.eAllContents();
					    	   AppMobile myApp = container.getMyProfile();
					    	   myApp.getAppName();
					    	   //ViewTransformation.getTemplateAndroid(mainView);
					    	   NewProjectCommand cmd = new NewProjectCommand();
					    	   cmd.setAppParam(myApp);
					    	   cmd.run();
	
					}
				} catch (Exception blah) {
				}				
				
				//(new NewProjectCommand()).newProject("NuevoProjectoTest");
  	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

}
